//
//  MKCards.m
//  Cards
//
//  Created by Mac on width_scale_value23/11/29.
//

#import "MKCards.h"
#import "MKUIView+Extension.h"

#define RandomColor [UIColor colorWithRed:(arc4random() % 256) / 255.0 \
                                     green:(arc4random() % 256) / 255.0 \
                                      blue:(arc4random() % 256) / 255.0 \
                                     alpha:1.0]

@interface MKCards()

//
@property(strong,nonatomic) NSArray *datas;
// 向上划走的数据
@property(strong,nonatomic) NSArray *haveRead;

@property(assign,nonatomic) NSInteger showCount;


@property(strong,nonatomic) NSMutableArray *views;
@property(strong,nonatomic) NSArray *below_views;
@property(strong,nonatomic) UIPanGestureRecognizer *panGesture;
@property(assign,nonatomic) CGPoint initialCenter;
@property(nonatomic,strong) UIView *temp_view;
@property(nonatomic,strong) UIView *current_view;


@end

@implementation MKCards

CGFloat cards_width = 0;
CGFloat cards_height = 0;


CGFloat xy_scale_value = 10;
CGFloat width_scale_value = 20;



-(instancetype)initWithFrame:(CGRect)frame datas:(NSArray *)datas cardCount:(NSInteger)shwoCount{
    if (self = [super initWithFrame:frame]){
        self.datas = datas;
        self.showCount = shwoCount;
        
        [self ui];
        
    }
    return self;
}


- (void)ui{
    self.views = [NSMutableArray array];
    

    cards_width = self.frame.size.width - width_scale_value * 2;
    cards_height = self.frame.size.height;
    
    self.temp_view = [[UIView alloc]init];
    self.temp_view.frame = CGRectMake(self.frame.size.width * 0.5 - cards_width * 0.5 - (self.showCount - 1) * xy_scale_value,
                                      -cards_height,
                                      cards_width + (self.showCount - 1) * width_scale_value,
                                      cards_height);
    
    
    self.temp_view.alpha = 0;
    self.temp_view.backgroundColor = [UIColor purpleColor];
    
    self.temp_view.layer.cornerRadius = 8;
    self.temp_view.layer.masksToBounds = NO;
    
    for (int i = 0; i < self.showCount; i++) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = RandomColor;
        view.layer.cornerRadius = 8;
        view.layer.masksToBounds = NO;
        [self addSubview:view];

        // Add shadow properties
        view.layer.shadowColor = [UIColor blackColor].CGColor;
        view.layer.shadowOffset = CGSizeMake(0, 8);
        view.layer.shadowOpacity = 0.2;
        view.layer.shadowRadius = 3;
        view.tag = i + 1;
        
        
        // 使用frame设置位置和大小
        
        view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                cards_width + i * width_scale_value,
                                cards_height);
        [self.views addObject:view];
        view.tag = i;
    }
    
    [self addSubview:self.temp_view];
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self addGestureRecognizer:self.panGesture];
}

// 拖动手势处理方法
- (void)handlePan:(UIPanGestureRecognizer *)gesture {
    
    CGPoint translation = [gesture translationInView:self];

    if (gesture.state == UIGestureRecognizerStateBegan) {
        self.current_view = nil;
        self.below_views = [NSArray array];
    }
    
    if(gesture.state == UIGestureRecognizerStateChanged){
        if (translation.y < 0){
            
            // 当前的view还未确定
            if (!self.current_view){
                self.current_view = self.views.lastObject;
                self.initialCenter = self.current_view.center;
                self.below_views = [self.views subarrayWithRange:NSMakeRange(0, self.views.count - 1)];
                
            }else{
                [self move_view:translation];
            }
            
            
        }else{
            
            // 当前的view还未确定
            if (!self.current_view){
                self.current_view = self.temp_view;
                self.initialCenter = self.current_view.center;
                self.below_views = self.views;
                
            }else{
                [self move_view:translation];
            }
        }
        
    }

    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        
        [self view_move_end:translation];
    }
}



- (void)move_view:(CGPoint)translation{
    //NSLog(@"%f",translation.y);
    
    

    
    CGFloat alpha = 0;
    CGFloat scale = 0;
    
    // 返回向下滑动
    if (self.below_views.count == self.showCount){
        self.current_view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y + translation.y);
        alpha = fabs(translation.y) / self.current_view.frame.size.height;
        scale = self.current_view.alpha;
        NSLog(@"3");
    }else{
    // 第一个view向上滑动
        alpha = 1 - fabs(translation.y) / self.current_view.frame.size.height;
        scale = 1 - self.current_view.alpha;
        
        // 第一个向上滑动再向下滑动时做出限制
//        if (self.current_view.center.y > self.initialCenter.y){
//            self.current_view.center = self.initialCenter;
//            NSLog(@"2");
//        }else{
//            self.current_view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y + translation.y);
//            NSLog(@"1");
//        }
//
        if (translation.y < 0){
            self.current_view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y + translation.y);
        }
    }
    
    //NSLog(@"%f",scale);
    
    
    self.current_view.alpha = alpha;
    
    [self.below_views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self view:obj scale:scale arrIndex:idx];
    }];
}


- (void)view:(UIView *)view scale:(CGFloat)scale arrIndex:(NSInteger)index{
    
    if (self.below_views.count == self.showCount){
        view.frame = CGRectMake(
                                (self.width - cards_width) * 0.5 - index * xy_scale_value + scale * xy_scale_value,
                                
                                self.height * 0.5 - index * xy_scale_value - cards_height * 0.5 + scale * xy_scale_value,
                                
                                cards_width + index * width_scale_value - scale * width_scale_value,
                                
                                cards_height);
    }else{
        view.frame = CGRectMake(
                                (self.width - cards_width) * 0.5 - index * xy_scale_value - scale * xy_scale_value,
                                
                                self.height * 0.5 - index * xy_scale_value - cards_height * 0.5 - scale * xy_scale_value,
                                
                                cards_width + index * width_scale_value + scale * width_scale_value,
                                
                                cards_height);
    }
}

- (void)view_move_end:(CGPoint)translation{
    
    
    
    
    if (self.current_view != self.temp_view){
        if (translation.y < -50){
            [UIView animateWithDuration:0.2 animations:^{
                self.current_view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y - self.current_view.frame.size.height);
                self.current_view.alpha = 0;
            }completion:^(BOOL finished) {
                if (finished){
                    [self.current_view removeFromSuperview];
                    [self insertSubview:self.current_view belowSubview:self.views.firstObject];
                    [self.views removeObject:self.current_view];
                    [self.views insertObject:self.current_view atIndex:0];
                    self.current_view.alpha = 1;
                    
                    for (int i = 0; i < self.views.count; i++) {
                        UIView *view = self.views[i];
                        if (i == 0){
                            view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                                    self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                                    cards_width + i * width_scale_value,
                                                    cards_height);
                        }else{
                            [UIView animateWithDuration:0.2 animations:^{
                                view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                                        self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                                        cards_width + i * width_scale_value,
                                                        cards_height);
                            }];
                        }
                    }
                }
            }];
        }else{
            [UIView animateWithDuration:0.25 delay:0
                            usingSpringWithDamping:1 // 调整这个值来改变弹簧效果的强度
                             initialSpringVelocity:0.1  // 调整这个值来改变弹簧效果的速度
                                           options:UIViewAnimationOptionCurveEaseInOut
                                        animations:^{
                self.current_view.center = self.initialCenter;
                self.current_view.alpha = 1;
            }
                             completion:nil];

            [self resetFrame];
        }
    }
    
    
    
    
    
    if (self.current_view == self.temp_view){
        if (translation.y > 100){
            [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                self.current_view.frame = CGRectMake((self.width - cards_width) * 0.5 - 2 * xy_scale_value,
                                                      self.height * 0.5 - 2 * xy_scale_value - cards_height * 0.5,
                                                      cards_width + 2 * width_scale_value,
                                                      cards_height);
                self.current_view.alpha = 1;
            }completion:^(BOOL finished) {
                self.temp_view = [self.views firstObject];
                [self.views addObject:self.current_view];
                self.temp_view.frame = CGRectMake(self.frame.size.width * 0.5 - cards_width * 0.5 - (self.showCount - 1) * xy_scale_value,
                                                  -cards_height,
                                                  cards_width + (self.showCount - 1) * width_scale_value,
                                                  cards_height);
                // 重新添加到最上面
                [self.temp_view removeFromSuperview];
                [self addSubview:self.temp_view];
                
                [self.views removeObjectAtIndex:0];
                
                
                self.temp_view.alpha = 0;
                
                for (int i = 0; i < self.views.count; i++) {
                    UIView *view = self.views[i];
                    if (i == 0){
                        view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                                self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                                cards_width + i * width_scale_value,
                                                cards_height);
                    }else{
                        [UIView animateWithDuration:0.2 animations:^{
                            view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                                    self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                                    cards_width + i * width_scale_value,
                                                    cards_height);
                        }];
                    }
                }
            }];
        }else{
            [UIView animateWithDuration:0.25 animations:^{
                self.current_view.frame = CGRectMake(self.frame.size.width * 0.5 - cards_width * 0.5 - (self.showCount - 1) * xy_scale_value,
                                                  -cards_height,
                                                  cards_width + (self.showCount - 1) * width_scale_value,
                                                  cards_height);
                self.temp_view.alpha = 0;
                
                [self resetFrame];
            }];
        }
    }

    
}



- (void)resetFrame{
    for (int i = 0; i < self.views.count; i++) {
        UIView *view = self.views[i];
        view.alpha = 1;
        view.frame = CGRectMake((self.width - cards_width) * 0.5 - i * xy_scale_value,
                                self.height * 0.5 - i * xy_scale_value - cards_height * 0.5,
                                cards_width + i * width_scale_value,
                                cards_height);
    }
}

@end
